/**
 * @(#)Rock.java
 *
 *
 * @author 
 * @version 1.00 2009/12/10
 */
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;

public class Rock 
{
	private static final int SPAWN_DISPLACEMENT = 5;
	
	private double xVelocity;
	private double yVelocity;
	private Ellipse2D.Double rockShape;
	private double xPosition;
	private double yPosition;
	private int size;

	public Rock(double xOrigin, double yOrigin, int newSize)
	{
		this((int)xOrigin, (int)yOrigin, newSize);
	}
    public Rock(int xOrigin, int yOrigin, int newSize) 
    {
    	xPosition = xOrigin;
    	yPosition = yOrigin;
    	size = newSize;
		rockShape = new Ellipse2D.Double(xPosition-size/2, yPosition-size/2, size, size);
		xVelocity = this.RandomNumber(0,2) * RandomSign();
		yVelocity = this.RandomNumber(0,2) * RandomSign();
    }
    
    static public int RandomNumber(int min, int max)
    {
    	return (int)Math.round((Math.random() * (max - min))) + min;
    }
    static public int MultiRangeRandom(int[] min, int[] max)
    {
    	try
    	{
    		if(min.length != max.length)
    		{
    			throw new NumberFormatException();
    		}
    	}
    	catch (NumberFormatException e)
    	{
    		return -1;
    	}
    	int chosenRange = Rock.RandomNumber(0, min.length-1);
    	return Rock.RandomNumber(min[chosenRange], max[chosenRange]);
    }
    public int RandomSign()
    {
    	if(Math.random()*2 > 1)
    	{
    		return -1;
    	}
    	else
    	{
    		return 1;
    	}
    }
    public Ellipse2D.Double getRockShape()
    {
    	return rockShape;
    }
    
    public void Update()
    {
    	xPosition += xVelocity;
    	yPosition -= yVelocity;
    	rockShape = new Ellipse2D.Double(xPosition-size/2, yPosition - size/2, size, size);
    	
    	//Wrap the asteroid around
    	
    	//Wrap the asteroid around
    	if(xPosition < 0)
    	{
    		xPosition += AsteroidsFrame.FRAME_WIDTH;
    	}
    	else if(xPosition > AsteroidsFrame.FRAME_WIDTH)
    	{
    		xPosition -= AsteroidsFrame.FRAME_WIDTH;
    	}
    	
    	if(yPosition < 0)
    	{
    		yPosition += AsteroidsFrame.FRAME_HEIGHT;
    	}
    	else if(yPosition > AsteroidsFrame.FRAME_HEIGHT)
    	{
    		yPosition -= AsteroidsFrame.FRAME_HEIGHT;
    	}


    }
    public boolean isShot(Shot s)
    {
    	if(rockShape.contains(s.getPositionPoint()))
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }
    public boolean isShot(Shot[] shots)
    {
    	for(int i = 0; i < shots.length; i++)
    	{	
	    	if(rockShape.contains(shots[i].getPositionPoint()))
	    	{
	    		return true;
	    	}
    	}
    	{
    		return false;
    	}
    }
    
    public boolean isShot(SuperShot[] superShots)
    {
    	
    	for(int i = 0; i < superShots.length; i++)
    	{	
	    	if(rockShape.contains(superShots[i].getPositionPoint()))
	    	{
	    		return true;
	    	}
    	}
    	{
    		return false;
    	}
    }
    public int whichShot(Shot[] shots)
    {
    	for(int i = 0; i < shots.length; i++)
    	{	
	    	if(rockShape.contains(shots[i].getPositionPoint()))
	    	{
	    		return i;
	    	}
    	}
    	{
    		return -1;
    	}
    }
    public int whichSuperShot(SuperShot[] superShots)
    {
    	for(int i = 0; i < superShots.length; i++)
    	{	
	    	if(rockShape.contains(superShots[i].getPositionPoint()))
	    	{
	    		return i;
	    	}
    	}
    	return -1;
    	
    }
    public void Delete()
    {
    	rockShape = null;
    }
    public Rock[] Spawn()
    {
    	Rock[] spawn = new Rock[2];
    	spawn[0] = new Rock(xPosition + (2*Math.random()-1)*SPAWN_DISPLACEMENT, yPosition + (2*Math.random()-1)*SPAWN_DISPLACEMENT, size - 20);
    	spawn[1] = new Rock(xPosition + (2*Math.random()-1)*SPAWN_DISPLACEMENT, yPosition + (2*Math.random()-1)*SPAWN_DISPLACEMENT, size - 20);
    	return spawn;
    }
    
    public int getSize()
    {
    	return size;
    }
    
    public double GetXPosition()
    {
    	return xPosition;
    }
    
    public double GetYPosition()
    {
    	return yPosition;
    }
    
    public Point2D.Double GetPosition()
    {
    	return new Point2D.Double(xPosition, yPosition);
    }
    
}